﻿using Coolzon.Realtime.ServerApp.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Server
{
    public static class ConsoleCMD
    {
        public static bool Execute(string cmdString)
        {
            string[] cmdStringArr = cmdString.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            if (cmdStringArr.Length > 0)
            {
                string cmd = cmdStringArr[0].ToUpper();

                if (cmd == "CLEAR")
                {
                    Console.Clear();
                    return false;
                }
                else if (cmd == "VERSION")
                {
                    ShowVersion();
                    return false;
                }
                else if (cmd == "EXIT")
                {
                    return true;
                }
                else if (cmd == "DOMAINLIST")
                {
                    ShowDomainList();
                    return false;
                }
                else if (cmd == "USERLIST")
                {
                    ShowUserList();
                    return false;
                }
            }
            return false;
        }

        private static void ShowDomainList()
        {
            Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}",
                "DomainId",
                "DomainCaption",
                "DomainIndex",
                "SolutionName",
                "SolutionCaption",
                "Operator",
                "ClientCount",
                "ClientList");
            DomainInstance[] domainList = ServerData.Instance.GetDomainList();
            for (int index = 0; index < domainList.Length; index++)
            {
                if (domainList[index] != null)
                {
                    string clientList = string.Empty;
                    foreach (var item in domainList[index].GetClientList())
                    {
                        clientList += item.UserIdentity + ";";
                    }
                    Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}",
                        domainList[index].Id,
                        domainList[index].Caption,
                        domainList[index].Index,
                        domainList[index].SolutionName,
                        domainList[index].SolutionCaption,
                        domainList[index].OperatorIdentity,
                        domainList[index].HasAnyClient(),
                        clientList);
                }
            }
        }

        private static void ShowUserList()
        {
            Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}",
                "Identity",
                "Account",
                "Index",
                "AppType",
                "State",
                "DomainId",
                "Status");
            Client[] clientList = ServerData.Instance.GetClientList();
            for (int index = 0; index < clientList.Length; index++)
            {
                if (clientList[index] != null)
                {
                    if (clientList[index].Domain == null)
                    {
                        Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}",
                            clientList[index].UserIdentity,
                            clientList[index].UserAccount,
                            clientList[index].Index,
                            clientList[index].AppType,
                            clientList[index].State,
                            "-",
                            clientList[index].Status);
                    }
                    else
                    {
                        Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}",
                            clientList[index].UserIdentity,
                            clientList[index].UserAccount,
                            clientList[index].Index,
                            clientList[index].AppType,
                            clientList[index].State,
                            clientList[index].Domain.Id,
                            clientList[index].Status);
                    }
                }
            }
        }

        private static void ShowVersion()
        {
            Assembly assembly = Assembly.GetEntryAssembly();
            Console.WriteLine("Version:v{0}",
                assembly.GetName().Version.ToString(4));
        }
    }
}
